package com.hcss.core.aspect;


import java.lang.reflect.Method;
import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hcss.core.annotation.SysOpertionLog;
import com.hcss.core.common.logsmgt.model.OpertionLog;
import com.hcss.core.common.logsmgt.service.OpertionLogService;
import com.hcss.core.mvc.BaseController;
import com.hcss.core.util.CommonUtil;




/**
 * @ClassName: OpertionLogAspect
 * @Description: TODO 操作日志切面
 * @author lvfang
 * @date 2013-12-9 下午12:08:28
 * 
 */
@Aspect
@Component("opertionLogAspect")
public class OpertionLogAspect extends BaseController {
	@Autowired
	private OpertionLogService opertionLogService;

	/** 
	* <p> anyMethod TODO定义切入点</p>  void 设定文件 
	*/
	@Pointcut("execution(* com.csi.esg.ibdmt.*.service.*.*(..))")
	private void anyMethod() {}

	/** 
	* <p> before TODO方法执行前调用</p>
	* @param joinpoint  void 设定文件 
	*/
	@Before("anyMethod()")
	public void before(JoinPoint joinpoint) {
		joinpoint.getArgs();
	}

	/** 
	* <p> afterReturning TODO方法执行后调用</p>
	* @param joinpoint  void 设定文件 
	*/
	@AfterReturning("anyMethod()")
	public void afterReturning(JoinPoint joinpoint) {
	}

	/** 
	* <p> afterReturn TODO方法执行完毕之后最终调用</p>
	* @param joinpoint  void 设定文件 
	*/
	@After("anyMethod()")
	public void afterReturn(JoinPoint joinpoint) {
		
		Method method = ((MethodSignature)joinpoint.getSignature()).getMethod();
		SysOpertionLog log = method.getAnnotation(SysOpertionLog.class);
		if(this.getUserInfo() != null){
			if (log != null) {
				OpertionLog logInfo = new OpertionLog();
				logInfo.setOpUserId(this.getUserInfo().getId());
				logInfo.setOpModel(log.opModel());
				logInfo.setOpFunction(log.opFunction());
				logInfo.setOpMark(log.opMark());
				logInfo.setOpTime(new Date());
				logInfo.setOpIp(CommonUtil.getRemortAddr(this.serverSequest));
				opertionLogService.addOpertionLog(logInfo);
			}
		}
	}
}
